package net.cattaka.teamredwhite.web;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import android.content.Context;
import android.content.res.Resources;

public class AssetsContent extends AbstractContent {
	private String mFile;
	private String mMime;
	
	
	
	public AssetsContent(Context context) {
		super(context);
	}

	@Override
	public void handle(String path, Map<String, String> params, InputStream in, OutputStream out) throws IOException {
		// 下準備、静的HTMLの長さの取得(良い方法ないんかな・・・？)
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		{
			InputStream assetsIn = null;
			try {
				getContext();
				getContext().getResources();
				getContext().getResources().getAssets();
				assetsIn = getContext().getResources().getAssets().open(mFile);
				int r;
				while ((r=assetsIn.read()) != -1) {
					bout.write((byte)r);
				}
			} finally {
				if (assetsIn != null) {
					assetsIn.close();
				}
			}
		}
		
		// ステータスラインとヘッダーの出力
		ContentWriter.outputResponseHeader(out, 200, mMime, bout.size());
		out.write("\n".getBytes());
		
		// 実データの出力
		out.write(bout.toByteArray());
	}

	public String getFile() {
		return mFile;
	}

	public void setFile(String mFile) {
		this.mFile = mFile;
	}

	public String getMime() {
		return mMime;
	}

	public void setMime(String mMime) {
		this.mMime = mMime;
	}
}
